package com.tju.resource.business.tb_client;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.resource.mapper.TbClientMapper;
import com.tju.resource.mapper.TbOrderMapper;
import com.tju.resource.model.TbClient;
import com.tju.resource.model.TbOrder;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.tool.tool.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 */
@Service
public class TbClientServiceImpl extends ServiceImpl<TbClientMapper, TbClient> implements TbClientService {

    @Autowired
    TbClientMapper tbClientMapper;

    @Autowired
    TbOrderMapper tbOrderMapper;

    /**
     * 查询
     *
     * @param parameters
     * @return
     */
    @Override
    public Result<Object> getClientAll(ClientParameters parameters) {
        try {
            QueryWrapper<TbClient> wrapper = new QueryWrapper<>();
            if (EmptyUtils.isNotEmpty(parameters.getOne())) wrapper.like("client_name", parameters.getOne());
            if (EmptyUtils.isNotEmpty(parameters.getTwo())) wrapper.like("client_code", parameters.getTwo());
            wrapper.orderByDesc("id");
            Page<TbClient> page = new Page<>(parameters.getPage(), parameters.getLimit());
            IPage<TbClient> iPage = tbClientMapper.selectPage(page, wrapper);
            return ResultUtil.success(ResultEnum.SUCCESS, iPage.getRecords(), iPage.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /**
     * 添加
     *
     * @param tbClient
     * @return
     */
    @Override
    @Transactional
    public Result<Object> addClientAll(TbClient tbClient) {
        QueryWrapper<TbClient> wrapper = new QueryWrapper<>();
        wrapper.eq("client_name", tbClient.getClientName()).or();
        wrapper.eq("client_code", tbClient.getClientCode());
        if (EmptyUtils.isEmpty(tbClientMapper.selectList(wrapper))) {
            if (tbClientMapper.insert(tbClient) > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, "添加成功!");
            } else
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "添加失败！");
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "客户已存在！");
    }

    /**
     * 删除
     *
     * @param tbClient
     * @return
     */
    @Transactional
    @Override
    public Result<Object> delClientAll(TbClient tbClient) {
        QueryWrapper<TbOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("client_id", tbClient.getId());
        if (EmptyUtils.isEmpty(tbOrderMapper.selectList(wrapper))) {
            if (tbClientMapper.deleteById(tbClient.getId()) > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, "删除成功");
            } else
                return ResultUtil.error(ResultEnum.UNKONW_ERROR);
        } else
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, "客户已存在订单，无法删除！");
    }

    /**
     * 修改
     *
     * @param tbClient
     * @return
     */
    @Transactional
    @Override
    public Result<Object> updClientAll(TbClient tbClient) {
        QueryWrapper<TbClient> wrapper = new QueryWrapper<>();
        wrapper.eq("client_name", tbClient.getClientName()).or();
        wrapper.eq("client_code", tbClient.getClientCode());
        List<TbClient> list = tbClientMapper.selectList(wrapper);
        if (list.size() == 1) {
            if (!tbClient.getId().equals(list.get(0).getId()))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "客户已存在！");
        } else if (list.size() == 2)
            if (!tbClient.getId().equals(list.get(1).getId()))
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "客户编号已存在！");
        if (tbClientMapper.updateById(tbClient) > 0) {
            return ResultUtil.success(ResultEnum.SUCCESS, "修改成功！");
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, "修改失败！");
    }
}
